//
//  DataCacher.swift
//  E-CycleForShop
//
//  Created by enghou on 17/3/2.
//  Copyright © 2017年 xyxorigation. All rights reserved.
//  暂时用UserDefaults实现

import UIKit

class DataCacher: NSObject {
    static let instance = DataCacher.init()
    
    class func shared()->DataCacher{
        return instance
    }
    
    func removeAll() -> Void {
        UserDefaults.standard.removeObject(forKey: "token")
        UserDefaults.standard.removeObject(forKey: "account")
        UserDefaults.standard.synchronize()
    }
    
    //手机问题相关
    func fillDistributionInfo(distribution : String) -> Void {
        //保存发行版本如 国行 港行
        UserDefaults.standard.setValue(distribution, forKey: "distribution")
        UserDefaults.standard.synchronize()
    }
    
    func distributionInfo() -> String {
        if let distribution = UserDefaults.standard.value(forKey: "distribution") as? String {
            return distribution
        }
        return ""
    }
    //手机问题相关
    
    func login() -> Void {
        UserDefaults.standard.setValue("login", forKey: "login")
        UserDefaults.standard.synchronize()
    }
    
    func firstLogin() -> Bool {
        if UserDefaults.standard.value(forKey: "login") != nil {
            return false
        }
        return true
    }
    
    func fillShopper(model : ShopperModelItem?,id : String) -> Void {
        //得到模型，将该商品ID与model绑定
        //将该对值归档到字典
        guard model != nil else {
            return
        }
        
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let absolutePath = path + "/" + "shopper.dat"
//        let fileManager = FileManager.default
//        if fileManager.fileExists(atPath: absolutePath) {
//            if var dict = NSKeyedUnarchiver.unarchiveObject(withFile: absolutePath) as? ShopperModelItem{
//                dict[id] = model!
//                let _ =  NSKeyedArchiver.archiveRootObject(dict, toFile: absolutePath)
//                return
//            }
//        }
//        let dict = [id:model!]
        if model != nil {
            let _ = NSKeyedArchiver.archiveRootObject(model!, toFile: absolutePath)
        }
    }
    
    
    
    func shopper(id : String) -> ShopperModelItem? {
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
        let absolutePath = path + "/" + "shopper.dat"
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: absolutePath) {
            if let m = NSKeyedUnarchiver.unarchiveObject(withFile: absolutePath) as? ShopperModelItem{
                return m
            }
        }
        return nil
    }
    
//    token	String	"B0A48CD4E79E5E4753BB73380636BF2D"	
    func saveModel(model : LoginModel) -> Void {
        if model.token.count > 0 {
            self.fillToken(token: model.token)//存token
        }
        if model.account.count > 0 {
            self.fillAccount(account: model.account)//存帐户
        }
        self.fillArea(area: model.area)//存地区
        self.fillCity(city: model.city)//存城市
        self.fillProvince(province: model.province)//存省份
        self.fillNumber(number: model.number)
        self.fillKind(kind: model.kind)
        self.fillHall_id(hall_id: model.hall_id)
        self.fillName(name: model.name)//营业厅名称
        self.fillId(id: model.id)//hallid
    }
    
    func fillId(id : Int) -> Void {
        UserDefaults.standard.set(id, forKey: "id")
        UserDefaults.standard.synchronize()
    }
    
    func id()->Int{
        if let id = UserDefaults.standard.value(forKey: "id") as? Int {
            return id
        }
        return -1
    }
    
    func fillName(name : String)->Void{
        UserDefaults.standard.set(name, forKey: "name")
        UserDefaults.standard.synchronize()
    }
    
    func fillPass(pass : String)->Void{
        UserDefaults.standard.set(pass, forKey: "password")
        UserDefaults.standard.synchronize()
    }
    
    func fillRemberPass(remember : Bool)->Void{
        UserDefaults.standard.set(remember, forKey: "remember")
        UserDefaults.standard.synchronize()
    }
    
    func rememberPass()->Bool{
        if let remember = UserDefaults.standard.value(forKey: "remember") as? Bool {
            return remember
        }
        return false
    }
    
    func pass()->String{
        if let pass = UserDefaults.standard.value(forKey: "password") as? String {
            return pass
        }
        return ""
    }
    
    func name() -> String {
        if let name = UserDefaults.standard.value(forKey: "name") as? String {
            return name
        }
        return ""
    }
    
    func hall_id() -> String {
        if let hall_id = UserDefaults.standard.value(forKey: "hall_id") as? Int {
            return String(hall_id)
        }
        return ""
    }
    
    func fillNumber(number : String) -> Void {
        UserDefaults.standard.set(number, forKey: "number")
        UserDefaults.standard.synchronize()
    }
    
    func number()->String{
        if let number = UserDefaults.standard.value(forKey: "number") as? String {
            return number
        }
        return ""
    }
    
    func fillProvince(province : String)->Void{
        UserDefaults.standard.set(province, forKey: "province")
        UserDefaults.standard.synchronize()
    }
    
    func fillHall_id(hall_id : Int) -> Void {
        UserDefaults.standard.set(hall_id, forKey: "hall_id")
        UserDefaults.standard.synchronize()
    }
    
    func province()->String {
        if let province = UserDefaults.standard.value(forKey: "province") as? String {
            return province
        }
        return ""
    }
    
    func fillCity(city : String)->Void {
        UserDefaults.standard.set(city, forKey: "city")
        UserDefaults.standard.synchronize()
    }
    
    func city()->String {
        if let city = UserDefaults.standard.value(forKey: "city") as? String {
            return city
        }
        return ""
    }
    
    func fillArea(area : String)->Void{
        UserDefaults.standard.set(area, forKey: "area")
        UserDefaults.standard.synchronize()
    }
    
    func area()->String{
        if let area = UserDefaults.standard.value(forKey: "area") as? String {
            return area
        }
        return ""
    }
    
    func fillKind(kind : Int) -> Void {
        UserDefaults.standard.set(kind, forKey: "kind")
        UserDefaults.standard.synchronize()
    }
    
    func kind() -> Int {
        if let kind = UserDefaults.standard.value(forKey: "kind") as? Int {
            return kind
        }
        return -1
    }
    
    func fillUName(name : String) -> Void {
        UserDefaults.standard.set(name, forKey: "uname")
        UserDefaults.standard.synchronize()
    }
    
    func uname()->String{
        if let nm = UserDefaults.standard.value(forKey: "uname") as? String {
            return nm
        }
        return ""
    }
    
    func fillAccount(account : String) -> Void {
        UserDefaults.standard.set(account, forKey: "account")
        UserDefaults.standard.synchronize()
    }
    
    func fillToken(token : String) -> Void {
        UserDefaults.standard.set(token, forKey: "token")
        UserDefaults.standard.synchronize()
    }
    
    func account() -> String {
        let account = UserDefaults.standard.value(forKey: "account") as? String
        if account != nil {
            return account!
        }
        return ""
    }
    
    func token() -> String {
        let token = UserDefaults.standard.value(forKey: "token") as? String
        if token != nil {
            return token!
        }
        return ""
    }
}
